describe('App.MovePreConfirmController', function() {
    var controller;
    var transfer;
    describe('prepareController', function() {
        beforeEach(function(){
            var t_moveHolderCtrl = App.MoveHolderController.create();
            App.MovePreConfirmController.reopen({
                init:function(){
                    this.set('controllers.moveHolder', t_moveHolderCtrl);
                }
            })
            controller = App.MovePreConfirmController.create();
        });

        it('button title got changed if otp is required', function() {
            spyOn(App.getSession(), 'getCurrentTransfer').andReturn(Ember.Object.create({
                isOTPRequired: true
            }));
            controller.prepareController();
            expect(controller.get('btnTitle')).toBe(controller.get('i18nData.eTacGenerateBtn'));
        });

        it('button title is confirm if otp is not required', function(){
            spyOn(App.getSession(), 'getCurrentTransfer').andReturn(Ember.Object.create({
                isOTPRequired: false
            }));
            controller.prepareController();
            expect(controller.get('btnTitle')).toBe(controller.get('i18nData.confirmBtn'));
        });

        it('show expanded notes if isExpandedNotes is true', function() {
            spyOn(App.getSession(), 'getCurrentTransfer').andReturn(Ember.Object.create({
                isOTPRequired: false,
                isExpandedNotes: true
            }));
            spyOn(controller, 'showHideNotes').andCallThrough();
            controller.prepareController();

            expect(controller.showHideNotes).toHaveBeenCalled();
            expect(controller.get('showNotes')).toBeTruthy();
        });
        it('show collapsed notes if isExpandedNotes is false', function() {
            spyOn(App.getSession(), 'getCurrentTransfer').andReturn(Ember.Object.create({
                isOTPRequired: false,
                isExpandedNotes: false
            }));
            spyOn(controller, 'showHideNotes');
            controller.prepareController();

            expect(controller.showHideNotes).not.toHaveBeenCalled();
            expect(controller.get('showNotes')).toBeFalsy();
        });
    });
    describe('continueAction', function() {
        beforeEach(function(){
            controller = App.MovePreConfirmController.create();
        });
        it('request ETAC if otp is required', function() {
            controller.set('transfer', Ember.Object.create({
                isOTPRequired: true
            }));

            spyOn(controller, 'showLoading');

            spyOn(App.api_transfer, 'generateEtac').andCallFake(function(transfer, callback){
                callback({}, {});
            });

            spyOn(controller, 'generateEtacCallback');

            controller.continueAction();

            expect(controller.showLoading).toHaveBeenCalled();
            expect(App.api_transfer.generateEtac).toHaveBeenCalled();
            expect(controller.generateEtacCallback).toHaveBeenCalled();
        });

        it('confirm transaction if otp is not required', function(){
            controller.set('transfer', Ember.Object.create({
                isOTPRequired: false
            }));

            spyOn(controller, 'showLoading');

            spyOn(App.api_transfer, 'confirmTransfer').andCallFake(function(transfer, callback){
                callback({}, {});
            });

            spyOn(controller, 'confirmTransferCallback');

            controller.continueAction();

            expect(controller.showLoading).toHaveBeenCalled();
            expect(App.api_transfer.confirmTransfer).toHaveBeenCalled();
            expect(controller.confirmTransferCallback).toHaveBeenCalled();
        });
    });

    describe('generateEtacCallback', function() {
        it('show etac screen if successful', function(){
            spyOn(controller, 'hasError').andReturn(false);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'transitionTo');

            controller.generateEtacCallback({}, {});

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(controller.transitionTo).toHaveBeenCalledWith('move.confirm');

        });
        it('go back to move home if there is error', function(){
            spyOn(controller, 'hasError').andReturn(true);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'transitionTo');

            controller.set('controllers', Ember.Object.create({
                alert: Ember.Object.create({
                    showAlert: function() {

                    }
                })
            }));

            spyOn(controller.get('controllers.alert'), 'showAlert').andCallFake(function(message, callback) {
                callback();
            });
            spyOn(App.RouteHistory, 'flush');

            controller.generateEtacCallback({}, BreezeException.create());

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(App.RouteHistory.flush).toHaveBeenCalled();
            expect(controller.transitionTo).toHaveBeenCalledWith('move.home');

        });
    });

    describe('ConfirmTransferCallback', function() {
        it('show Summary screen if successful', function(){
            spyOn(controller, 'hasError').andReturn(false);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'transitionTo');

            controller.confirmTransferCallback({}, {});

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(controller.transitionTo).toHaveBeenCalledWith('move.summary');

        });
        it('stay in the same page if there is error', function(){
            spyOn(controller, 'hasError').andReturn(true);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'transitionTo');

            controller.confirmTransferCallback({}, {});

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(controller.transitionTo).not.toHaveBeenCalled();

        });
    });
});